﻿
<?php
require("_config-rating.php");

if (isset($_GET["page"])) { 
	$page  = $_GET["page"]; 
} else { 
	$page=1; 
}; 

if (isset($_GET["c"])) { 
	$c  = $_GET["c"]; 
}

/*if (isset($_GET["category"])) 
{ 
	$category  = $_GET["category"]; 
} else { 
	$category="home"; 
};

if($category=="latest")
{
	$pageInclude = 16;
	$direction="details.php";
}
else 
{
	$pageInclude = 30;
	$direction="index.php";
}*/

$category="home";
$pageInclude = 30;
$direction="index.php";

if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

mysql_query("SET NAMES 'utf8'");
$start_from = ($page-1) * $pageInclude ; 

if($c!=null)
	$sql = "SELECT * FROM games g where g.cId=$c order by addedDate desc  LIMIT $start_from, $pageInclude"; 
else
	$sql = "SELECT * FROM games g order by addedDate desc  LIMIT $start_from, $pageInclude"; 
$rs_result = mysql_query ($sql,$con); 
if($rs_result ){
	$outputUi = $outputUi.'<div  style="height:100%; margin-bottom:50px;margin-top:-20px;">';
	while ($row = mysql_fetch_array($rs_result)) { 
	
	$outputUi =$outputUi. '<div class="fbx"> <img hspace="10px" style="margin-bottom:3px;margin-left:20px;" align="center" src="'.$row["picUrl"].'" width="50" height="50"/>';
	$outputUi=$outputUi.'<span class="fbx-tiny">'.$row["gameName"].'</span>';
	$outputUi=$outputUi.'<span class="fbx-tiny"><a href="play.php?gameid='.$row["gameId"].'&game='.$row["gameName"].'">Hemen Oyna</a></span></div>';
	}
	$outputUi =$outputUi.'</div>';
	//pagination($con,$pageInclude,$page,$outputUi);	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////PAGINATION
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$sql = "SELECT COUNT(*) FROM games ";
	$rs_result = mysql_query($sql,$con); 
	if ($row = mysql_fetch_array($rs_result)){
		
	$total_records = $row[0]; 
	$total_pages = ceil($total_records / $pageInclude); 
	$outputUi=$outputUi.'<div style="margin-bottom:30px;margin-top:30px;">';
	$outputUi=$outputUi.'<div align="center" ><ol class="wp-paginate"><li><span class="title"></span></li>';
	
	$startPage=1;
	$pageCount=2;
	if ($pageCount>=$total_pages){
		$startPage=1;
		$loopCount=$total_pages;
	}else if($page+$pageCount<=$total_pages){
		if ($page>1)$startPage=$page-1;
		else $startPage=1;
		$loopCount=$startPage+$pageCount;
	}else if($page+$pageCount>$total_pages){
		$startPage=$total_pages-$pageCount;
		$loopCount=$startPage+$pageCount;
	}
	/*if($page+$pageCount<=$total_pages && $page-1>0){
		$startPage=$page-1;
	}else if($page+$pageCount>$total_pages){
		$startPage=$total_pages-$pageCount;
	}*/
	
	if(($page-1)>0){
		$outputUi=$outputUi.'<li><a class="next" href="'.$direction.'?page='.($page-1).'&category='.$category.'.&c='.$c.'">«</a></li>';
	}
	for ($i=$startPage; $i<=$loopCount; $i++) { 
		if($page==$i){
			$outputUi=$outputUi.'<li><span class="page current">'.$i.'</span></li>';
		}else{
			$outputUi=$outputUi.'<li><a class="page" title='.$i.' href="'.$direction.'?page='.$i.'&category='.$category.'.&c='.$c.'">'.$i.'</a></li>';
		}
		//echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
	};
	if(($page+1)<=$total_pages){
		$outputUi=$outputUi.'<li><a class="next" href="'.$direction.'?page='.($page+1).'&category='.$category.'.&c='.$c.'">»</a></li>';
	}
	
	$outputUi=$outputUi.'</ol></div></div>';
	}
	echo $outputUi;
}
mysql_close();


function pagination($con,$pageInclude,$page,$outputUi){
	$sql = "SELECT COUNT(*) FROM games ";
	$rs_result = mysql_query($sql,$con); 
	if ($row = mysql_fetch_array($rs_result)){
		
	$total_records = $row[0]; 
	$total_pages = ceil($total_records / $pageInclude); 
	$outputUi=$outputUi.'<div>';
	$outputUi=$outputUi.'<div align="center" ><ol class="wp-paginate"><li><span class="title"></span></li>';
	
	$startPage=1;
	$pageCount=2;
	if ($pageCount>=$total_pages){
		$startPage=1;
		$loopCount=$total_pages;
	}else if($page+$pageCount<=$total_pages){
		if ($page>1)$startPage=$page-1;
		else $startPage=1;
		$loopCount=$startPage+$pageCount;
	}else if($page+$pageCount>$total_pages){
		$startPage=$total_pages-$pageCount;
		$loopCount=$startPage+$pageCount;
	}
	/*if($page+$pageCount<=$total_pages && $page-1>0){
		$startPage=$page-1;
	}else if($page+$pageCount>$total_pages){
		$startPage=$total_pages-$pageCount;
	}*/
	
	if(($page-1)>0){
		$outputUi=$outputUi.'<li><a class="next" href="'.$direction.'?page='.($page-1).'&category='.$category.'">«</a></li>';
	}
	for ($i=$startPage; $i<=$loopCount; $i++) { 
		if($page==$i){
			$outputUi=$outputUi.'<li><span class="page current">'.$i.'</span></li>';
		}else{
			$outputUi=$outputUi.'<li><a class="page" title='.$i.' href="'.$direction.'?page='.$i.'&category='.$category.'">'.$i.'</a></li>';
		}
		//echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
	};
	if(($page+1)<=$total_pages){
		$outputUi=$outputUi.'<li><a class="next" href="'.$direction.'?page='.($page+1).'&category='.$category.'">»</a></li>';
	}
	
	$outputUi=$outputUi.'</ol></div></div>';
	}

}

function ajax_utf_temizle($dizi) {
 return is_array($dizi) ? array_map('ajax_utf_temizle', $dizi) : iconv("UTF-8","ISO-8859-9",$dizi);
}
?>  